//
//  KKTabBar.swift
//  KKWeibo
//
//  Created by kassia on 16/7/9.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit


class KKTabBar: UITabBar {

     //  点击撰写按钮执行的闭包
    var composeButtonClosure: (()->())?
    
    private lazy var composeButton: UIButton = {
    
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)//设置图片
        
        //设置选中图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)//设置背景
    
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Normal)//设置选中背景
        
        button.addTarget(self, action: "composeButtonAction", forControlEvents: .TouchUpInside)//添加点击事件
        
        button.sizeToFit()
        
        return button
    }()
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()//调用刷新UI的界面
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
    addSubview(composeButton)//添加到界面
    
    }
    
    @objc private func composeButtonAction() {
    
        print("哈哈哈")
        composeButtonClosure?()//执行闭包
    
    }
    
    //在layoutSubviews里面设置其frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置中心的X,Y
        composeButton.centerX = weight * 0.5
        composeButton.centerY = height * 0.5

        var index = 0//计数
        
        let itemWeight = weight/5//每个的uandu
        
        for value in subviews {//循环设置x
        
            if value .isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                value.weight = itemWeight//每个barItem的宽
                
                value.x = CGFloat(index) * itemWeight//每个barItem对应的x
                
                index++
                
                if index == 2{
                
                   index++
                }
            }
        
        }
        
        

        
        
        
        
        
    }

}
